package cn.schoolwow.quickdao.module.database.dql.response.flow.common.resultSet;

import cn.schoolwow.quickdao.domain.database.parent.DatabaseType;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.sql.ResultSet;

public class SetResultSetArrayFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        DatabaseType databaseType = flowContext.checkInstanceData(DatabaseType.class);
        ResultSet resultSet = (ResultSet) flowContext.checkData("resultSet");
        Long count = (Long) flowContext.getData("count");
        Property[] resultSetPropertyArray = (Property[]) flowContext.checkData("resultSetPropertyArray");

        JSONArray resultSetArray = (null==count)?new JSONArray():new JSONArray(count.intValue());
        while (resultSet.next()) {
            JSONObject resultSetObject = new JSONObject();
            for(Property property:resultSetPropertyArray){
                if(null==property.clazz){
                    resultSetObject.put(property.columnLabel, resultSet.getObject(property.columnLabel));
                    continue;
                }
                switch (property.clazz.getName()){
                    case "java.util.Date":{
                        resultSetObject.put(property.columnLabel, resultSet.getDate(property.columnLabel));
                    }break;
                    case "com.alibaba.fastjson.JSONObject":{
                        String jsonString = resultSet.getString(property.columnLabel);
                        resultSetObject.put(property.columnLabel, JSON.parseObject(jsonString));
                    }break;
                    case "com.alibaba.fastjson.JSONArray":{
                        String jsonString = resultSet.getString(property.columnLabel);
                        resultSetObject.put(property.columnLabel, JSON.parseArray(jsonString));
                    }break;
                    default:{
                        switch (databaseType){
                            case SQLite:
                            case Postgresql:{
                                resultSetObject.put(property.columnLabel, resultSet.getObject(property.columnLabel));
                            }break;
                            default:{
                                Object value = resultSet.getObject(property.columnLabel);
                                if(null!=value){
                                    resultSetObject.put(property.columnLabel, resultSet.getObject(property.columnLabel, property.clazz));
                                }
                            }
                        }
                    }break;
                }
            }
            resultSetArray.add(resultSetObject);
        }
        flowContext.putData("resultSetArray", resultSetArray);
    }

    @Override
    public String name() {
        return "设置查询结果集";
    }
}
